package gui;

import database.worker.DBTrackWorker;
import dto.Metadata;
import dto.Track;
import exceptions.TrackException;
import java.awt.Toolkit;
import java.io.File;
import java.util.Calendar;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import map.MapManager;
import resources.ResourceManager;

/**
 * A Window (Dialog) to create / modify a track
 * 
 * @author Pascal
 */
public class AddEditTrack extends javax.swing.JDialog {

    /**
     * Creates new form AddEditTrack
     */
    private Track newTrack = null;
    boolean isNew = true;
    private float distance;

    public AddEditTrack() {
        this(null);

    }

    public AddEditTrack(Track track) {
        initComponents();

        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/gui/resources/logo.png")));

        isNew = track == null;

        if (isNew) {
            this.newTrack = new Track();
            setTitle(ResourceBundle.getBundle("gui/guilabels").getString("addtrack"));
        } else {
            this.newTrack = track;
            btChoosePath.setEnabled(false);
            setTitle(ResourceBundle.getBundle("gui/guilabels").getString("edittrack"));
            loadTrack(newTrack);
        }
    }

    public Track getTrack() {
        return newTrack;
    }

    private void loadTrack(dto.Track track) {
        txtName.setText(track.getTrackName());
        txtAreaDescription.setText(track.getMetadata().getDescription());
        txtGroundConditions.setText(track.getMetadata().getGroundConditions());
        txtHotels.setText(track.getMetadata().getHotels());
        txtRastaurant.setText(track.getMetadata().getRestaurants());
        txtRestingPlaces.setText(track.getMetadata().getRestingPlaces());
        txtSigns.setText(track.getMetadata().getSigns());
        txtTrackProfile.setText(track.getMetadata().getProfile());

        dateChooser.setDate(track.getTrackDate().getTime());
        timeBegin.setDate(track.getDate(), true);
        Calendar end = track.getDate();
        end.add(Calendar.MILLISECOND, (int) track.getMetadata().getTime());
        timeEnd.setDate(end, true);

        distance = track.getMetadata().getDistance();

        comboBustle.setSelectedItem(track.getMetadata().getBustle());
        comboParking.setSelectedItem(track.getMetadata().getParking());
        comboWeather.setSelectedItem(track.getMetadata().getWeather());

        spinnerCosts.setValue(track.getMetadata().getExpenses());
        spinnerTemp.setValue(track.getMetadata().getTemperature());
        spinnerDistanceHome.setValue(track.getMetadata().getDistanceToHome());

        if (track.getMetadata().getRating() != null) {
            switch (track.getMetadata().getRating()) {
                case LOW:
                    radioEasy.setSelected(true);
                    break;
                case MEDIUM:
                    radioMedium.setSelected(true);
                    break;
                case HIGH:
                    radioHard.setSelected(true);
                    break;
            }
        }

        cbSuitableForDogs.setSelected(track.getMetadata().isSuitableForDogs());

        txtGPXPath.setEnabled(false);
        txtGPXPath.setText(track.getGpxFilePath());
        btChoosePath.setEnabled(false);
    }

    private void saveTrack() {

        try {
            if (isNew) {
                newTrack.setMetadata(new Metadata());
            }

            newTrack.setName(txtName.getText());

            if (!txtAreaDescription.getText().isEmpty()) {
                newTrack.getMetadata().setDescription(txtAreaDescription.getText());
            }
            if (!txtGroundConditions.getText().isEmpty()) {
                newTrack.getMetadata().setGroundConditions(txtGroundConditions.getText());
            }
            if (!txtHotels.getText().isEmpty()) {
                newTrack.getMetadata().setHotels(txtHotels.getText());
            }
            if (!txtRastaurant.getText().isEmpty()) {
                newTrack.getMetadata().setRestaurants(txtRastaurant.getText());
            }
            if (!txtRestingPlaces.getText().isEmpty()) {
                newTrack.getMetadata().setRestingPlaces(txtRestingPlaces.getText());
            }
            if (!txtSigns.getText().isEmpty()) {
                newTrack.getMetadata().setSigns(txtSigns.getText());
            }
            if (!txtTrackProfile.getText().isEmpty()) {
                newTrack.getMetadata().setProfile(txtTrackProfile.getText());
            }

            newTrack.getMetadata().setDistance(distance);

            Calendar begin = dateChooser.getCalendar();
            begin.set(Calendar.HOUR_OF_DAY, timeBegin.getDate().get(Calendar.HOUR_OF_DAY));
            begin.set(Calendar.MINUTE, timeBegin.getDate().get(Calendar.MINUTE));

            Calendar end = dateChooser.getCalendar();
            end.set(Calendar.HOUR_OF_DAY, timeEnd.getDate().get(Calendar.HOUR_OF_DAY));
            end.set(Calendar.MINUTE, timeEnd.getDate().get(Calendar.MINUTE));

            newTrack.setDate(begin);

            newTrack.getMetadata().setTime(end.getTimeInMillis() - begin.getTimeInMillis());

            newTrack.getMetadata().setBustle(comboBustle.getSelectedItem().toString());
            newTrack.getMetadata().setParking(comboParking.getSelectedItem().toString());
            newTrack.getMetadata().setWeather(comboWeather.getSelectedItem().toString());

            newTrack.getMetadata().setExpenses((float) spinnerCosts.getValue());
            newTrack.getMetadata().setTemperature((float) spinnerTemp.getValue());
            newTrack.getMetadata().setDistanceToHome((float) spinnerDistanceHome.getValue());

            if (radioEasy.isSelected()) {
                newTrack.getMetadata().setRating(Metadata.RATING.LOW);
            } else if (radioMedium.isSelected()) {
                newTrack.getMetadata().setRating(Metadata.RATING.MEDIUM);
            } else if (radioHard.isSelected()) {
                newTrack.getMetadata().setRating(Metadata.RATING.HIGH);
            }

            newTrack.getMetadata().setSuitableForDogs(cbSuitableForDogs.isSelected());

            if (isNew) {
                ResourceManager resourceMan = new ResourceManager();
                newTrack.setGpxFilePath(resourceMan.copyGPXToProperties(txtGPXPath.getText()));

                DBTrackWorker worker = new DBTrackWorker(DBTrackWorker.Type.SaveTrack, newTrack);
                worker.execute();
            } else {
                DBTrackWorker worker = new DBTrackWorker(DBTrackWorker.Type.UpdateTrack, newTrack);
                worker.execute();
            }
        } catch (TrackException ex) {
            JOptionPane.showMessageDialog(this,
                    ResourceBundle.getBundle("gui/guilabels").getString("errorsavingtrack") + "\n" + ex.getLocalizedMessage(),
                    ResourceBundle.getBundle("gui/guilabels").getString("error"),
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private boolean checkMustHaveFields() {

        if (txtName.getText().isEmpty()) {
            return false;
        }

        if (txtGPXPath.getText().isEmpty() && isNew) {
            return false;
        }

        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        radioGroupDiff = new javax.swing.ButtonGroup();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtRastaurant = new javax.swing.JTextField();
        txtRestingPlaces = new javax.swing.JTextField();
        dateChooser = new com.toedter.calendar.JDateChooser();
        timeBegin = new gui.picker.TimeFieldPicker();
        timeEnd = new gui.picker.TimeFieldPicker();
        radioEasy = new javax.swing.JRadioButton();
        radioMedium = new javax.swing.JRadioButton();
        radioHard = new javax.swing.JRadioButton();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        cbSuitableForDogs = new javax.swing.JCheckBox();
        txtHotels = new javax.swing.JTextField();
        comboBustle = new javax.swing.JComboBox();
        spinnerCosts = new javax.swing.JSpinner();
        txtGroundConditions = new javax.swing.JTextField();
        spinnerTemp = new javax.swing.JSpinner();
        comboParking = new javax.swing.JComboBox();
        txtSigns = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaDescription = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        txtGPXPath = new javax.swing.JTextField();
        btChoosePath = new javax.swing.JButton();
        txtTrackProfile = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        comboWeather = new javax.swing.JComboBox();
        jLabel22 = new javax.swing.JLabel();
        spinnerDistanceHome = new javax.swing.JSpinner();
        jLabel23 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btInterupt = new javax.swing.JButton();
        btSave = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();

        setModal(true);
        setPreferredSize(new java.awt.Dimension(600, 650));

        jPanel1.setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/guilabels"); // NOI18N
        jLabel1.setText(bundle.getString("namestar")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText(bundle.getString("datestar")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText(bundle.getString("beginstar")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel3, gridBagConstraints);

        jLabel4.setText(bundle.getString("endstar")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel4, gridBagConstraints);

        jLabel5.setText(bundle.getString("trackprofile")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel5, gridBagConstraints);

        jLabel6.setText(bundle.getString("difficulty")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel6, gridBagConstraints);

        jLabel8.setText(bundle.getString("description")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 20;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel8, gridBagConstraints);

        jLabel9.setText(bundle.getString("hotels")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel9, gridBagConstraints);

        jLabel10.setText(bundle.getString("restaurant")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel10, gridBagConstraints);

        jLabel11.setText(bundle.getString("restingplaces")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel11, gridBagConstraints);

        jLabel12.setText(bundle.getString("groundconditions")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel12, gridBagConstraints);

        jLabel13.setText(bundle.getString("peopleontrack")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel13, gridBagConstraints);

        jLabel14.setText(bundle.getString("parkingpossibilities")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel14, gridBagConstraints);

        jLabel15.setText(bundle.getString("cost")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 14;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel15, gridBagConstraints);

        jLabel16.setText(bundle.getString("sign")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 15;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel16, gridBagConstraints);

        jLabel17.setText(bundle.getString("temperature")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 16;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel17, gridBagConstraints);

        jLabel18.setText(bundle.getString("gpxfilestar")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 19;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel18, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtName, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtRastaurant, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtRestingPlaces, gridBagConstraints);

        dateChooser.setCalendar(java.util.Calendar.getInstance());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(dateChooser, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(timeBegin, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(timeEnd, gridBagConstraints);

        radioGroupDiff.add(radioEasy);
        radioEasy.setSelected(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel1.add(radioEasy, gridBagConstraints);

        radioGroupDiff.add(radioMedium);
        radioMedium.setToolTipText("");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 42, 0, 4);
        jPanel1.add(radioMedium, gridBagConstraints);

        radioGroupDiff.add(radioHard);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 79, 0, 4);
        jPanel1.add(radioHard, gridBagConstraints);

        jLabel19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/yellow_point.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 64, 0, 4);
        jPanel1.add(jLabel19, gridBagConstraints);

        jLabel20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/red_point.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 101, 0, 4);
        jPanel1.add(jLabel20, gridBagConstraints);

        jLabel21.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/green_point.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 26, 0, 4);
        jPanel1.add(jLabel21, gridBagConstraints);

        cbSuitableForDogs.setText(bundle.getString("dogssuitablewc")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 1, 4, 4);
        jPanel1.add(cbSuitableForDogs, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtHotels, gridBagConstraints);

        comboBustle.setModel(new javax.swing.DefaultComboBoxModel(new String[] { bundle.getString("nobustle"), bundle.getString("lessbustle"), bundle.getString("mediumbustle"), bundle.getString("muchbustle") }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(comboBustle, gridBagConstraints);

        spinnerCosts.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(0.0f), Float.valueOf(0.0f), null, Float.valueOf(0.5f)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 14;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(spinnerCosts, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtGroundConditions, gridBagConstraints);

        spinnerTemp.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(15.0f), null, null, Float.valueOf(0.5f)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 16;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(spinnerTemp, gridBagConstraints);

        comboParking.setModel(new javax.swing.DefaultComboBoxModel(new String[] { bundle.getString("no"), bundle.getString("less"), bundle.getString("medium"), bundle.getString("much") }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 13;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(comboParking, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 15;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtSigns, gridBagConstraints);

        txtAreaDescription.setColumns(20);
        txtAreaDescription.setLineWrap(true);
        txtAreaDescription.setRows(5);
        jScrollPane1.setViewportView(txtAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 21;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jScrollPane1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.BorderLayout());

        txtGPXPath.setEditable(false);
        jPanel2.add(txtGPXPath, java.awt.BorderLayout.CENTER);

        btChoosePath.setText("...");
        btChoosePath.setMinimumSize(new java.awt.Dimension(25, 23));
        btChoosePath.setPreferredSize(new java.awt.Dimension(25, 23));
        btChoosePath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btChoosePathActionPerformed(evt);
            }
        });
        jPanel2.add(btChoosePath, java.awt.BorderLayout.LINE_END);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 19;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jPanel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(txtTrackProfile, gridBagConstraints);

        jLabel7.setText(bundle.getString("weather")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 17;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel7, gridBagConstraints);

        comboWeather.setModel(new javax.swing.DefaultComboBoxModel(new String[] { bundle.getString("sunny"), bundle.getString("cloudy"), bundle.getString("rainy") }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 17;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(comboWeather, gridBagConstraints);

        jLabel22.setText(bundle.getString("distancehome")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 18;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel22, gridBagConstraints);

        spinnerDistanceHome.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(0.0f), Float.valueOf(0.0f), null, Float.valueOf(0.5f)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 18;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(spinnerDistanceHome, gridBagConstraints);

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel23.setText(bundle.getString("requiredfields")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 22;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel1.add(jLabel23, gridBagConstraints);

        jScrollPane2.setViewportView(jPanel1);

        getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        btInterupt.setText(bundle.getString("cancel")); // NOI18N
        btInterupt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btInteruptActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(btInterupt, gridBagConstraints);

        btSave.setText(bundle.getString("save")); // NOI18N
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(btSave, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel3.add(jPanel4, gridBagConstraints);

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        if (!checkMustHaveFields()) {

            JOptionPane.showMessageDialog(getParent(), ResourceBundle.getBundle("gui/guilabels").getString("isRequired"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
        } else if (timeBegin.getDate().compareTo(timeEnd.getDate()) > 0) {
            JOptionPane.showMessageDialog(getParent(), ResourceBundle.getBundle("gui/guilabels").getString("endbeforebegin"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
        } else {

            saveTrack();
            this.setVisible(false);
        }


    }//GEN-LAST:event_btSaveActionPerformed

    private void btInteruptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInteruptActionPerformed
        newTrack = null;
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_btInteruptActionPerformed

    private void btChoosePathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btChoosePathActionPerformed
        final JFileChooser chooser = new JFileChooser("Verzeichnis wählen");
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".gpx") || f.isDirectory();
            }

            public String getDescription() {
                return "GPX (*.gpx)";
            }
        });
        chooser.setMultiSelectionEnabled(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                distance = MapManager.getDistanceByGPXFile(chooser.getSelectedFile().getPath());

                txtGPXPath.setText(chooser.getSelectedFile().getPath());
            } catch (Exception e) {
                //problem parsing/opening gpx file
                JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("errorparsinggpx"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.ERROR_MESSAGE);
            }

        }
    }//GEN-LAST:event_btChoosePathActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btChoosePath;
    private javax.swing.JButton btInterupt;
    private javax.swing.JButton btSave;
    private javax.swing.JCheckBox cbSuitableForDogs;
    private javax.swing.JComboBox comboBustle;
    private javax.swing.JComboBox comboParking;
    private javax.swing.JComboBox comboWeather;
    private com.toedter.calendar.JDateChooser dateChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton radioEasy;
    private javax.swing.ButtonGroup radioGroupDiff;
    private javax.swing.JRadioButton radioHard;
    private javax.swing.JRadioButton radioMedium;
    private javax.swing.JSpinner spinnerCosts;
    private javax.swing.JSpinner spinnerDistanceHome;
    private javax.swing.JSpinner spinnerTemp;
    private gui.picker.TimeFieldPicker timeBegin;
    private gui.picker.TimeFieldPicker timeEnd;
    private javax.swing.JTextArea txtAreaDescription;
    private javax.swing.JTextField txtGPXPath;
    private javax.swing.JTextField txtGroundConditions;
    private javax.swing.JTextField txtHotels;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtRastaurant;
    private javax.swing.JTextField txtRestingPlaces;
    private javax.swing.JTextField txtSigns;
    private javax.swing.JTextField txtTrackProfile;
    // End of variables declaration//GEN-END:variables
}
